﻿Imports System.Data.Linq
Imports System.Linq

Public Class FileMBLRepository
    Private dc As New TanNamChinhDataContext

    Public Sub New()
        dc = GeneralClassDAL.dc
    End Sub

    Public Function SelectAllRecords() As IQueryable(Of tblFileMBL)
        Return dc.tblFileMBLs
    End Function

    Public Function SelectRecordById(ByVal id As Integer) As tblFileMBL
        Dim fileMBL = dc.tblFileMBLs.SingleOrDefault(Function(p As tblFileMBL) p.ID_FileMBL = id)
        Return fileMBL
    End Function

    Public Sub Insert(ByVal obj As tblFileMBL)
        dc.tblFileMBLs.InsertOnSubmit(obj)
        dc.SubmitChanges()
    End Sub

    Public Sub Delete(ByVal obj As tblFileMBL)
        dc.tblFileMBLs.DeleteOnSubmit(obj)
        dc.SubmitChanges()
    End Sub

    Public Sub Update(ByVal obj As tblFileMBL)
        'Dim obj As tblFileMBL = dc.tblFileMBLs.SingleOrDefault(Function(p As tblFileMBL) p.ID_FileMBL = objFileMBL.ID_FileMBL)
        'dc.tblFileMBLs.Attach(objFileMBL)
        dc.Refresh(RefreshMode.KeepChanges, obj)
        dc.SubmitChanges(ConflictMode.ContinueOnConflict)
    End Sub

    Public Function GetFileMBLList(ByVal id_file As Integer) As IQueryable(Of tblFileMBL)
        Dim query As IQueryable(Of tblFileMBL) = From p In dc.tblFileMBLs
                                  Where p.ID_File = id_file
                                  Select p
        Return query
    End Function

    Public Function GetMBLList(ByVal id_file As Integer) As IQueryable(Of tblMBL)
        Dim query As IQueryable = From p In dc.tblFileMBLs
                                  Where p.ID_File = id_file
                                  Select p.tblMBL
        'Select p.tblMBL.ID_MBL, p.tblMBL.MBLNo


        Return query
    End Function

    ''' <summary>
    ''' Update: 14-11-2013
    ''' Do 1 File chỉ có 1 MBL nên sẽ lấy top 1 nếu tồn tại
    ''' </summary>
    ''' <param name="id_file"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetMBLByFile(ByVal id_file As Integer) As tblFileMBL
        Dim query As IQueryable = (From p In dc.tblFileMBLs
                                  Where p.ID_File = id_file
                                  Select p).Take(1)
        Dim objMBL As tblFileMBL = Nothing
        For Each obj As tblFileMBL In query
            objMBL = obj
        Next
        Return objMBL
    End Function

    Public Function SelectRecordById(ByVal id_File As Integer, ByVal id_MBL As Integer) As tblFileMBL
        Dim fileMBL = dc.tblFileMBLs.SingleOrDefault(Function(p As tblFileMBL) p.ID_File = id_File And p.ID_MBL = id_MBL)
        Return fileMBL
    End Function
End Class
